from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QPushButton

class CountdownButton(QPushButton):
    """带倒计时功能的按钮（用于获取验证码）。"""
    def __init__(self, text='获取验证码', parent=None):
        super().__init__(text, parent)
        self._orig_text = text
        self._count = 0
        self._timer = QTimer(self)
        self._timer.timeout.connect(self._tick)

    def start_countdown(self, seconds=60):
        if self._timer.isActive():
            return
        self._count = seconds
        self.setEnabled(False)
        self.setText(f'{self._count}s')
        self._timer.start(1000)

    def _tick(self):
        self._count -= 1
        if self._count <= 0:
            self._timer.stop()
            self.setEnabled(True)
            self.setText(self._orig_text)
        else:
            self.setText(f'{self._count}s')
